package cn.itcast.shiro.poi;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;


import java.io.FileOutputStream;
import java.io.IOException;

public class App {
    @Test
    public void write() throws IOException {
        //创建工作簿 excel对象
        XSSFWorkbook workbook = new XSSFWorkbook();

        //创建工作表
        XSSFSheet sheet = workbook.createSheet("测试");

        //创建行
        XSSFRow row = sheet.createRow(0);

        //创建单元格

        XSSFCell cell = row.createCell(0);

        //设置第一行第一列内容
        cell.setCellValue("第一行第一列内容");

        workbook.write(new FileOutputStream("d:/test.xlsx"));


    }

    @Test
    public void read() throws IOException {
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook("d:/test.xlsx");
        //获取工作表
        XSSFSheet sheet = workbook.getSheet("测试");
        //获取第一行
        XSSFRow row = sheet.getRow(0);
        //获取第一行第一列
        XSSFCell cell = row.getCell(0);

        //获取单元格输出
        System.out.println("单元格cell="+cell.getStringCellValue());
        System.out.println("获取总行数"+ sheet.getPhysicalNumberOfRows());
        System.out.println("获取某一行的总列数" + row.getPhysicalNumberOfCells());
    }
}
